package com.zld.smarket.service.serviceImpl;

import com.zld.smarket.dao.GoodsTypeMapper;
import com.zld.smarket.pojo.GoodsType;
import com.zld.smarket.result.ResponseData;
import com.zld.smarket.service.GoodsTypeServive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;
@Service
public class GoodsTypeServiceImpl implements GoodsTypeServive {
    @Autowired
    private GoodsTypeMapper goodsTypeMapper;
    @Override
    public ResponseData findList(Integer page, Integer limit) {
        if(page != null && limit!=null){
            page = (page-1)*limit;
        }else{
            page = 0;
            limit = 10;
        }
        List<GoodsType> goodsTypes = goodsTypeMapper.selectAllAndPage(page,limit);
        Long count = goodsTypeMapper.countType();
        return new ResponseData(goodsTypes,count);
    }

    @Override
    public List<GoodsType> selectAll() {
        return goodsTypeMapper.selectAll();
    }

    @Override
    public Integer addType(GoodsType goodsType) {
        int j=0;
        j=goodsTypeMapper.insert(goodsType);
        return j;
    }

    @Override
    public ResponseData search(String s,Integer page, Integer limit) {
        if(page != null && limit!=null){
            page = (page-1)*limit;
        }else{
            page = 0;
            limit = 10;
        }
        List<GoodsType> goodsTypes =  goodsTypeMapper.search(s,page,limit);
        Long count = Long.valueOf(goodsTypes.size());
        return new ResponseData(goodsTypes,count);
    }

    @Override
    public Long selectbyno(Long typeNo) {
        return goodsTypeMapper.countTypeByNo(typeNo);
    }

    @Override
    public Integer deleteById(Long[] ids) {
        int j=0;
        try {
            //先判定数据是否存在
            for (Long id : ids) {
                GoodsType goodsType = goodsTypeMapper.selectByPrimaryKey(id);
                if(goodsType != null) {
                    j = goodsTypeMapper.deleteByPrimaryKey(id);
                }
            }
        }catch(Exception e){
            e.printStackTrace();
//            设置手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return j;
    }
}
